package cn.jiyun.service.impl;

import cn.jiyun.dao.OrderSettingDao;
import cn.jiyun.pojo.OrderSetting;
import cn.jiyun.serivce.OrderSettingService;
import cn.jiyun.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void importData(List<String[]> dataList) throws Exception {
        for (String[] data : dataList) {
            //2、封装实体类
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(DateUtils.parseString2Date(data[0],"yyyy/MM/dd"));
            orderSetting.setNumber(Integer.parseInt(data[1]));
            //3、保存数据库
            orderSettingDao.add(orderSetting);
        }

    }

    @Override
    public List<Map<String, Object>> getDataByOrderDate(String orderDate) {
        return orderSettingDao.getDataByOrderDate(orderDate);
    }

    @Override
    public void setNumberByOrderDate(String orderDate, Integer number) throws Exception {
        //先根据日期查询
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if(orderSetting==null){
            //查不到添加操作
            orderSetting = new OrderSetting();
            orderSetting.setOrderDate(DateUtils.parseString2Date(orderDate));
            orderSetting.setNumber(number);

            orderSettingDao.add(orderSetting);

        }else {
            //查到做修改操作
            orderSetting.setNumber(number);
            orderSettingDao.update(orderSetting);
        }
    }
}
